<!--
 * @Author: your name
 * @Date: 2021-09-06 15:27:55
 * @LastEditTime: 2021-09-09 10:23:54
 * @LastEditors: LAPTOP-JN5U4VA6
 * @Description: In User Settings Edit
 * @FilePath: \ej-webui\src\pages\system\index.vue
-->
<template>
  <div class="system">
    <div class="btns">
      <!-- 检索框 -->
      <el-form :inline="true" :model="params" size="small">
        <el-button @click="addHandler" type="primary" size="small" style="margin-right: 10px" >新增</el-button>
      </el-form>
    </div>
    <div class="content">
      <!-- 表格 -->
      <el-table :data="resData" style="width: 100%" size="small">
        <el-table-column label="产品编号" prop="id"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="值" prop="val">
          <template v-slot="scope">
            <img
                slot="reference"
                :src="scope.row.val"
                width="50"
                alt="图片丢失"
              />
          </template>
        </el-table-column>
        <el-table-column label="介绍" prop="introduce">
          <template v-slot="scope">{{scope.row.introduce}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="editHandler(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delHandler(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- total表示：数据的总条数  current-page表示：当前所在的页数   -->
      <el-pagination
        background
        layout="prev, pager, next, sizes"
        :total="resData.total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="pageChangeHandler"
        :page-sizes="[5, 10, 15]"
        @size-change="pageSizeChangeHandler"
      >
      </el-pagination>
      <!-- 模态框 -->
      <el-dialog :title="title" :visible.sync="visible" @close="dialogCloseHandler">
        <el-form  :model="system" :rules="rules" ref="saveForm">
          <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="system.name"></el-input>
          </el-form-item>
          <el-form-item label="介绍" :label-width="formLabelWidth" prop="introduce">
            <el-input v-model="system.introduce"></el-input>
          </el-form-item>
          <el-form-item label="类型" :label-width="formLabelWidth" >
            <el-select v-model="system.value">
              <el-option label="文字" value="文字" selected></el-option>
              <el-option label="图片" value="图片"></el-option>
            </el-select>
          </el-form-item>
          <!-- prop 表示的是父子组件之间传参   -->
          <el-form-item v-show="system.value == '文字'" label="值" :label-width="formLabelWidth" >
            <el-input v-model="system.val"></el-input>
          </el-form-item>
          <el-form-item v-show="system.value == '图片'"  label="图片" :label-width="formLabelWidth">
            <el-upload
              v-model="system.val"
              class="avatar-uploader"
              action="http://81.69.24.232/pet/jz_upload.php"
              :show-file-list="false"
              :on-success="handlePhotoSuccess">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSaveHandler">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";
import { mapActions } from "vuex";
import qs from "qs";
import _ from "lodash";

export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 5,
      },
      resData: [],
      title: "",
      system: {},
      visible: false,
      formLabelWidth: '80px',
      imageUrl: null,
      rules: {
        name: [
          { required: true, message: "产品名称不能为空", trigger: "blur"},
        ],
        introduce: [
          {
            required: true,
            message: "产品介绍不能为空",
            trigger: "blur"
          }
        ],
      }

    };
  },
  methods: {
        // 导入状态机中的方法
        ...mapActions("category", ["findAllCategories"]),

        // 分页查询产品表格
        async pageQueryProductions() {
            // 过滤数据将空字符串删除
            for (let key in this.params) {
                if (this.params[key] == "") {
                    delete this.params[key];
                }
            }
            let res = await request.get("/baseConfig/findAll", {
                params: this.params,
            });
            this.resData = res.data;
        },
        // page改变事件  
        pageChangeHandler(page) {
            // 将更新了的页码传递给页面   并且刷新查询到的对应页码上的内容
            this.params.page = page;
            this.pageQueryProductions();
        },
        // pageSize改变事件  pageSize是每页中显示的数据条目
        pageSizeChangeHandler(pageSize) {
            this.params.pageSize = pageSize;
            this.pageQueryProductions();
        },

        // 新增按钮  处理函数
        addHandler(){
            this.system = {};
            this.imageUrl = null;
            this.title = '新增商品';
            this.visible = true;
        },
        // 添加完成后的保存事件
        toSaveHandler(){
            this.$refs["saveForm"].validate((valid) => {
                if(valid) {
                    request.post("/baseConfig/saveOrUpdate", qs.stringify(this.system))
                        .then((res) => {
                        this.pageQueryProductions(); // 重载数据
                        this.$message.success(res.message); // 提示操作结果信息
                        this.visible = false; // 关闭模态框
                    });
                } else {
                    return fasle;
                }
            });
        },
        // 关闭模态框的回调函数
        dialogCloseHandler() {
            // 置空表单验证
            this.$refs["saveForm"].resetFileds();
        },
        // 图片上传成功回调
        handlePhotoSuccess(res) {
            // 将返回的图片在线地址映射到数据模型中
            this.system.val = res;
            this.imageUrl = res;
        },

        // 编辑按钮  事件函数
        editHandler(row) {
            // 将当前行数据赋值给数据模型
            this.system = _.clone(row);
            this.imageUrl = row.val;
            this.val = row.val;
            this.title = "编辑基础配置";
            this.visible = true;
        },
        // 删除 事件
        delHandler(id) {
            this.$confirm('此操作将永久删除该基础配置, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                request.get('/baseConfig/deleteById',{ params:{id} }).then((res) => {
                    // 刷新数据
                    this.pageQueryProductions();
                    this.$message({
                        type: "success",
                        message: res.message,
                    }); // 提示成功信息
                })

            })
        },
    },
    created() {
        this.pageQueryProductions(); // 分页查询产品
        // this.findAllCategories().then((res) => {
        //     this.categories = res.data;
        // }); // 查询所有产品栏目
        
        this.findAllCategories();
    },
};
</script>

<style scoped>
/* 图片上传  样式 */
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>